/**
 *
 * 一个适用于相机控制的类CameraControls.js
 *
 *  Daniel
 *
 * 2018.03.07
 *
 *
 * */

define(["OrbitControls",'PointerLockControls'],function(){
    var CameraCotrol = function(){
        this.orbitCamera = {};                  //Orbit控制器对象
        this.viewCamera = {};                   //Free控制器对象
        this.cameraControl = {
                moveUp : false,
                moveDown : false,
                moveForward : false,
                moveBackward : false,
                moveLeft : false,
                moveRight : false,
                prevTime : performance.now()
        };
        this.numberData = 1;
        this.element = document.body;
        this.enabled = true;
    };
    
    //初始化
    CameraCotrol.prototype.init = function(camera){
        var scope = this;
        this.havePointerLockA(camera);
        
        document.addEventListener("keydown",onKeyDown,false);
        document.addEventListener("keyup",onKeyUp,false);
        
        //键盘事件监听
        //(摁下)
        function onKeyDown(event){
            switch (event.keyCode)
            {
                //w
                case 87:
                    scope.cameraControl.moveForward = true;
                    break;
                //a
                case 65:
                    scope.cameraControl.moveLeft = true;
                    break;
                //s
                case 83:
                    scope.cameraControl.moveBackward = true;
                    break;
                //d
                case 68:
                    scope.cameraControl.moveRight = true;
                    break;
                //q
                case 81:
                    scope.cameraControl.moveUp = true;
                    break;
                //e
                case 69:
                    scope.cameraControl.moveDown = true;
                    break;
                //r
                case 82:
                    console.log(scope.orbitCamera.object.position);
                    break;
                    
                //f
                case 70:
                
                    break;
                //left
                case 37:
                
                    break;
                //up
                case 38:
                
                    break;
                //right
                case 39:
                
                    break;
                //down
                case 40:
                
                    break;
                //ctrl
                case 17:
                    break;
                //ctrl
                case 1:
                    console.log("我是一个鼠标按键");
                    break;
            
            }
        }
        
        //(抬起)
        function onKeyUp(event) {
            switch (event.keyCode)
            {
                //w
                case 87:
                    scope.cameraControl.moveForward = false;
                    break;
                //a
                case 65:
                    scope.cameraControl.moveLeft = false;
                    break;
                //s
                case 83:
                    scope.cameraControl.moveBackward = false;
                    break;
                //d
                case 68:
                    scope.cameraControl.moveRight = false;
                    break;
                //q
                case 81:
                    scope.cameraControl.moveUp = false;
                    break;
                //e
                case 69:
                    scope.cameraControl.moveDown = false;
                    break;
                //r
                case 82:
                
                    break;
                //f
                case 70:
                
                    break;
                //left
                case 37:
                
                    break;
                //up
                case 38:
                
                    break;
                //right
                case 39:
                
                    break;
                //down
                case 40:
                
                    break;
                //ctrl
                case 17:
                    break;
            }
        
        }
    };
    
    //轨道相机控制器(第三视角)
    CameraCotrol.prototype.cameraOrbit = function(newCamera, element){
        var cameras = newCamera;
        var controls = new THREE.OrbitControls(cameras, element);
        controls.target.set(cameras.looks.x, cameras.looks.y, cameras.looks.z);
        controls.enableKeys = false;
        controls.enabled = this.enabled;
        cameras.orbitControls = controls;
        controls.name = cameras.name;
        this.orbitCamera = controls;
        if(cameras.hawk) {
            controls.enableZoom = false;
            controls.enablePan = false;
        }
        //this.originalOrbitP = JSON.stringify(cameras.toJSON());
        //cameras.originalOrbitP = this.originalOrbitP;
        return controls;
    };
    
    //自由相机控制器
    CameraCotrol.prototype.cameraView = function(camera,scene){
        var controls = new THREE.PointerLockControls(camera);
        var cameraObject = controls.getObject();
        cameraObject.position.copy(camera.position);
        camera.freeControl = controls;
        camera.freeControl.walkControl = false;
        scene.add(cameraObject);
        this.viewCamera = controls;
        this.init(camera);
        //this.originalFreeP = JSON.stringify(camera.toJSON());
        //camera.originalFreeP = this.originalFreeP;
        return controls
    };
    
    // 第一视角调用
    CameraCotrol.prototype.freeCamera = function (){          //传入相机或者编号
        var scope = this;
        return function(){
            scope.viewCamera.walkControl = true;
            scope.element.requestPointerLock = scope.element.requestPointerLock || scope.element.mozRequestPointerLock || scope.element.webkitRequestPointerLock;
            scope.element.requestPointerLock();
        };
       
    };
    
    //获取浏览器的无指针状态
    CameraCotrol.prototype.havePointerLockA = function (camera){                         //controlsArray传入要获取鼠标的相机控制器
        var havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
        if (havePointerLock)
        {
            var scope = this;
            var pointerlockchange = function (event)
            {
                if (document.pointerLockElement === scope.element || document.mozPointerLockElement === scope.element || document.webkitPointerLockElement === scope.element)
                {
                    //指针激活——第一视角开启
                    if (camera)
                    {
                        scope.numberData = scope.cameraClutch(camera);
                    }
                } else
                {
                        scope.numberData = scope.cameraClutch(camera);
                        //Vm.exitFullscreen();
                }
            };
            var pointerlockerror = function (event)
            {
                console.warn("Sorry! It is not defined!");
            };
            
            // Hook pointer lock state change events
            document.addEventListener('pointerlockchange', pointerlockchange, false);
            document.addEventListener('mozpointerlockchange', pointerlockchange, false);
            document.addEventListener('webkitpointerlockchange', pointerlockchange, false);
            document.addEventListener('pointerlockerror', pointerlockerror, false);
            document.addEventListener('mozpointerlockerror', pointerlockerror, false);
            document.addEventListener('webkitpointerlockerror', pointerlockerror, false);
        }
        else
        {
            console.error("当前不支持该模式");
        }
    };
    
    //相机离合器
    CameraCotrol.prototype.cameraClutch = function(camera){
        var free = camera.freeControl.enabled;
        if (free == true)
        {
            camera.freeControl.enabled = false;
            camera.freeControl.walkControl = false;
            this.orbitCamera.enabled = true;
            return numberData = 1;
        } else
        {
            camera.freeControl.enabled = true;
            camera.freeControl.walkControl = true;
            this.orbitCamera.enabled = false;
            return numberData = 2;
        }
        
    };
    
    //游览控制器
    CameraCotrol.prototype.renderFreeCamera = function (freeControls, parameters){
        if (parameters === undefined){
            var time = window.performance.now();
            var parameters = {
                delta: (time - this.cameraControl.prevTime) / 200,
                velocity: new THREE.Vector3(),
                contA: 10,
                gravity: 9.8,
                deviator: 0.05,
                contFdelta: 180.0,
                contDelta: 180.0,
            }
        }
        var control = freeControls;
        var scope = this;
        return function(){
            if (control.walkControl)
            {
                parameters.velocity.x -= parameters.velocity.x * parameters.contA * parameters.delta;
                parameters.velocity.z -= parameters.velocity.z * parameters.contA * parameters.delta;
                //if ( cameraControl.moveGravity ) parameters.velocity.y -= parameters.gravity * parameters.deviator * parameters.delta; 		//有重力						// 100.0 = mass
                if (scope.cameraControl.moveUp) parameters.velocity.y += parameters.contDelta * parameters.delta;
                if (scope.cameraControl.moveDown) parameters.velocity.y -= parameters.contDelta * parameters.delta;
                if (scope.cameraControl.moveForward) parameters.velocity.z -= parameters.contFdelta * parameters.delta;
                if (scope.cameraControl.moveBackward) parameters.velocity.z += parameters.contDelta * parameters.delta;
                if (scope.cameraControl.moveLeft) parameters.velocity.x -= parameters.contDelta * parameters.delta;
                if (scope.cameraControl.moveRight) parameters.velocity.x += parameters.contDelta * parameters.delta;
    
                control.getObject().translateX(parameters.velocity.x * parameters.delta);
                control.getObject().translateY(parameters.velocity.y * parameters.delta);
                control.getObject().translateZ(parameters.velocity.z * parameters.delta);
                scope.cameraControl.prevTime = time;
            }
        }
       
    };
   
    return CameraCotrol;



});